<template>
  <div class="container">
    <el-form :inline="true" :model="formInline">
      <el-form-item label="版本名称">
        <el-input v-model="formInline.name" placeholder="请输入版本名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button type="primary" @click="dialogVisible=true" plain>新增版本</el-button>
        <el-button @click="init">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData.records" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" />
      <el-table-column prop="name" label="版本名称" align="center" min-width="150" />
      <el-table-column prop="versionNum" label="版本号" align="center" min-width="150" />
      <el-table-column prop="realName" label="附件" align="center" min-width="200" show-overflow-tooltip>
        <template #default="scope">
          <el-button type="primary" text @click="openUrl(scope.row.versionFile)">{{scope.row.versionFile}}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="updateNum" label="更新次数" align="center" />
      <el-table-column label="是否强制更新" align="center" min-width="150">
        <template #default="scope">
          {{scope.row.isForceUpgrade==0?"是":"否"}}
        </template>
      </el-table-column>
      <el-table-column label="更新内容" align="center" min-width="200" prop="versionContent" />
      <el-table-column label="发布时间" align="center" min-width="200">
        <template #default="scope">
          {{proxy.$utils.dateTimeFormateFn(scope.row.publishDate,"all")}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" min-width="200">
        <template #default="scope">
          <el-button type="primary" text @click="handleClick(scope.row,1)" icon="Edit">编辑</el-button>
          <el-button type="primary" text @click="handleClick(scope.row,2)" icon="Delete">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-if="tableData['total']" :total="tableData['total']" v-model:page="formInline.pageNum" v-model:limit="formInline.pageSize" @pagination="getInfo" />

    <el-dialog v-model="dialogVisible" title="新增/编辑版本" width="800" :before-close="handleClose">
      <el-form label-width="100" :model="form" :rules="rules" ref="formEl">
        <el-form-item label="版本名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入版本名称" clearable />
        </el-form-item>
        <el-form-item label="附件" prop="versionFile">
          <uploadFile v-model="form.versionFile" showTip="0"></uploadFile>
        </el-form-item>
        <el-form-item label="版本号" prop="versionNum">
          <el-input v-model="form.versionNum" placeholder="请输入版本号" clearable />
        </el-form-item>
        <el-form-item label="是否强制升级">
          <el-radio-group v-model="form.isForceUpgrade">
            <el-radio :label="0">是</el-radio>
            <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="更新次数" prop="updateNum">
          <el-input-number v-model="form.updateNum" :min="1" />
        </el-form-item>
        <el-form-item label="版本描述" prop="versionContent">
          <el-input v-model="form.versionContent" placeholder="请输入版本描述" type="textarea" :autosize="{ minRows : 4 }" />
        </el-form-item>
        <!-- <el-form-item label="发布时间">
          <DatePicker v-model="form.publishDate" type="datetime"></DatePicker>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="loading">
            确 认
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from "vue";

const { proxy } = getCurrentInstance();
let dialogVisible = ref(false);
let formInline = ref({
  name: "",
  pageNum: 1,
  pageSize: 10,
});
let tableData = ref({});
let loading = ref(false);
let ids = [];
let form = ref({
  name: "",
  //publishDate: proxy.$utils.dateTimeFormateFn(Date.now(), "all"),
  updateNum: 0,
  isForceUpgrade: 1,
  versionNum: null,
  versionFile: null,
  versionContent: null,
});
const rules = reactive<FormRules<RuleForm>>({
  name: [{ required: true, message: "请输入版本名称", trigger: "blur" }],
  versionFile: [{ required: true, message: "请上传附件", trigger: "blur" }],
  versionNum: [{ required: true, message: "请输入版本号", trigger: "blur" }],
  updateNum: [{ required: true, message: "请输入更新次数", trigger: "blur" }],
  versionContent: [
    { required: true, message: "请输入版本描述", trigger: "blur" },
  ],
  publishDate: [{ required: true, message: "请选择发布时间", trigger: "blur" }],
});

onMounted(() => {
  search();
});

let search = () => {
  formInline.value.pageNum = 1;
  getInfo();
};
let getInfo = async () => {
  tableData.value = await proxy.$apis.versionPage(formInline.value);
};
let init = () => {
  formInline.value = {
    name: "",
    pageNum: 1,
    pageSize: 10,
  };
  getInfo();
};
let handleSelectionChange = (array) => {
  let arr = [];
  array.forEach((element) => {
    arr.push(element.id);
  });
  ids = [...arr];
};
let handleClick = async (row, num) => {
  switch (num) {
    case 1:
      let resp = await proxy.$apis.versionDetail({ id: row.id });
      form.value = proxy.$utils.formatFunction(resp, { ...form.value });
      form.value.id = row.id;
      dialogVisible.value = true;
      break;
    case 2:
      ids = [row.id];
      delFn();
      break;
    default:
      break;
  }
};
let delFn = async () => {
  let resp = await proxy.$utils.confirmTips("是否确定删除?");
  if (!resp) return;
  for (let index = 0; index < ids.length; index++) {
    const element = ids[index];
    await proxy.$apis.versionDel({ id: element });
  }
  proxy.$utils.showToast("删除成功");
  getInfo();
};
let openUrl = (e) => {
  window.location.href = e;
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!proxy.$refs.formEl) return;
  await proxy.$refs.formEl.validate((valid, fields) => {
    if (valid) {
      save();
    } else {
      console.log("error submit!", fields);
    }
  });
};
let save = async () => {
  try {
    loading.value = true;
    if (form.value.id) {
      await proxy.$apis.versionUpdate(form.value);
    } else {
      await proxy.$apis.versionInsert(form.value);
    }
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
};
let handleClose = () => {
  form.value = {
    name: "",
    //publishDate: proxy.$utils.dateTimeFormateFn(Date.now(), "all"),
    updateNum: 0,
    isForceUpgrade: 1,
    versionNum: null,
    versionFile: null,
    versionContent: null,
  };
  proxy.$refs.formEl.resetFields();
  getInfo();
  dialogVisible.value = false;
};
</script>



<style lang="scss" scoped>
.container {
  margin: 20px;
}
</style>
